{%- set regex_list = []                                                           %}
{%- for filter in input_params.get('interface_filters', {})                       %}
{%-     if filter.get('op') == "regex"                                            %}
{%-         set x=regex_list.append(filter.get('expr', None))                     %}
{%-     endif                                                                     %}
{%- endfor                                                                        %}

{%- set regex_str='|'.join(regex_list)                                            %}
{%- set interface_type=input_params.get('interface_type', 'physical')             %}

{%- if command_resp is mapping                                                    %}
{%-     set command_resp = [command_resp]                                         %}
{%- endif                                                                         %}

{%- set interface_stats_list = []                                                 %}
{%- for physical_interface in command_resp                                        %}
{%-     if physical_interface.name | regex_search(regex_str|default(".*",true))   %}
{%-         if interface_type == "physical"                                       %}
{%-             set interface_stat = {}                                           %}
{%-             set x = interface_stat.update({
                            "name":physical_interface.name,
                            "op_status":physical_interface.get('oper-status', ""),
                            "admin_status": physical_interface.get('admin-status', ""),
                            "input_rate": physical_interface.get('traffic-statistics', {}).get('input-bps', ""),
                            "output_rate": physical_interface.get('traffic-statistics', {}).get('output-bps', "")
                                               })                                 %}
{%-             set y = interface_stats_list.append(interface_stat)               %}
{%-         elif interface_type == "logical"                                      %}
{%-             if 'unit' in physical_interface                                   %}
{%-                 if physical_interface.get('unit') is mapping                  %}
{%-                     set log_units = [physical_interface['unit']]              %}
{%-                 else                                                          %}
{%-                     set log_units = physical_interface['unit']                %}
{%-                 endif                                                         %}
{%-                 for log_interface in log_units                                %}
{%-                     set interface_stat = {}                                   %}
{%-                     set x = interface_stat.update({
                                    "name":physical_interface.name+'.'+log_interface.name,
                                    "op_status":log_interface.get('oper-status', ""),
                                    "admin_status": log_interface.get('admin-status', ""),
                                    "input_rate": log_interface.get('traffic-statistics', {}).get('input-bps', ""),
                                    "output_rate": log_interface.get('traffic-statistics', {}).get('output-bps', "")
                                                       })                         %}
{%-                     set y = interface_stats_list.append(interface_stat)       %}
{%-                 endfor                                                        %}
{%-             endif                                                             %}
{%-         endif                                                                 %}
{%-     endif                                                                     %}
{%- endfor                                                                        %}

{"interface_stats": {{interface_stats_list | to_json}} }

